import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        if (args.length > 0) {
            DirScan(args[0]);
        } else {
            DirScan(System.getProperty("user.dir"));
        }
    }

    public void testR() throws IOException {
        Excel_reader test = new Excel_reader();
        ArrayList<ArrayList<String>> arr = test.xlsx_reader("C:\\Users\\ch\\Desktop\\worktest2.xlsx", 0, 1, 2);  //后面的参数代表需要输出哪些列，参数个数可以任意
        for (int i = 0; i < arr.size(); i++) {
            ArrayList<String> row = arr.get(i);
            for (int j = 0; j < row.size(); j++) {
                System.out.print(row.get(j) + " ");
            }
            System.out.println("");
        }
    }

    public void testW() throws IOException {

        Map<String, List<List<ExcelData>>> data = new LinkedHashMap<String, List<List<ExcelData>>>();
        List<List<ExcelData>> sheet1 = new ArrayList<List<ExcelData>>();//第一页

        List<ExcelData> list2 = new ArrayList<ExcelData>();//第二行
        ExcelData excelData = new ExcelData();//第一个单元格
        excelData.setColSpan(1);
        excelData.setRowSpan(1);
        excelData.setValue("13601011234");
        list2.add(excelData);
        excelData = new ExcelData();//第二个单元格
        excelData.setColSpan(1);
        excelData.setRowSpan(1);
        excelData.setValue("张三");
        list2.add(excelData);
        excelData = new ExcelData();//第三个单元格
        excelData.setColSpan(1);
        excelData.setRowSpan(1);
        excelData.setValue("公安局");
        list2.add(excelData);

        sheet1.add(list2);

        data.put("表1", sheet1);

        Excel_reader.testWrite(new FileOutputStream(new File("C:\\Users\\ch\\Desktop\\work\\my.xlsx")), "xlsx", data);
    }

    public static void DirScan(String path) {
        //提示
        System.out.println("path:" + path);
        //
        Excel_reader test = new Excel_reader();
        int AMOUNT = 0;//总数
        HashSet<User> allusers = new HashSet<>();
        // 此处路径指定到目录而不是单个文件
        String defaultpath = "";//当前文件夹
        File file = new File(path.equals("") ? defaultpath : path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                // 对文件进行过滤，只读取Excel文件，非Excel文件不读取，否则会出错
                if (f.getName().indexOf(".xlsx") > 0) {
                    //assert
                    System.out.println(f.toString());
                    try {
                        ArrayList<ArrayList<String>> arr = test.xlsx_reader(f.toString(), 0, 1, 2);  //后面的参数代表需要输出哪些列，参数个数可以任意
                        for (int i = 0; i < arr.size(); i++) {
                            User tmpuser = new User(arr.get(i).get(0), arr.get(i).get(1), arr.get(i).get(2));
                            allusers.add(tmpuser);
                            AMOUNT++;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            System.out.println("请输入正确文件夹！");
        }
        //遍历与写出
        int BATCH = 50000;//50w
        int outname = 1;
        Map<String, List<List<ExcelData>>> data = new LinkedHashMap<String, List<List<ExcelData>>>();
        List<List<ExcelData>> sheet = new ArrayList<List<ExcelData>>();
        List<ExcelData> linedata = null;
        //
        Iterator<User> it = allusers.iterator();
        while (it.hasNext()) {
            if (BATCH-- <= 0) {
                BATCH = 50000;
                try {
                    data.put("表1", sheet);
                    String outpath = path + "\\" + outname++ + ".xlsx";
                    System.out.println("write:" + outpath);
                    Excel_reader.testWrite(new FileOutputStream(new File(outpath)), "xlsx", data);
                    linedata.clear();
                    sheet.clear();
                    data.clear();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            } else {
                User user = it.next();
                //generate
                linedata = new ArrayList<ExcelData>();
                ExcelData excelData = new ExcelData();//第一个单元格
                excelData.setColSpan(1);
                excelData.setRowSpan(1);
                excelData.setValue(user.getTel());
                linedata.add(excelData);
                excelData = new ExcelData();//第二个单元格
                excelData.setColSpan(1);
                excelData.setRowSpan(1);
                excelData.setValue(user.getName());
                linedata.add(excelData);
                excelData = new ExcelData();//第三个单元格
                excelData.setColSpan(1);
                excelData.setRowSpan(1);
                excelData.setValue(user.getHome());
                linedata.add(excelData);
                sheet.add(linedata);
            }
        }
        //收尾工作
        try {
            data.put("表1", sheet);
            String outpath = path + "\\" + outname++ + ".xlsx";
            System.out.println("write:" + outpath);
            Excel_reader.testWrite(new FileOutputStream(new File(outpath)), "xlsx", data);
        } catch (IOException e) {
            e.getStackTrace();
        }
    }

}